<?php

/**
 * This is the model class for table "tbl_invoice_emp".
 *
 * The followings are the available columns in table 'tbl_invoice_emp':
 * @property integer $id
 * @property string $invoice
 * @property string $counter
 * @property string $credit
 * @property string $employeeno
 * @property string $regularhours
 * @property string $overtimehours
 * @property string $laboramount
 * @property string $overhead
 * @property string $dispatch
 */
class InvoiceEmp extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return InvoiceEmp the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_invoice_emp';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('invoice, credit, employeeno, regularhours, overtimehours, laboramount, overhead', 'required'),
			array('invoice, credit, regularhours, overtimehours, laboramount, overhead', 'length', 'max'=>10),
			array('counter', 'length', 'max'=>3),
			array('employeeno', 'length', 'max'=>4),
			array('dispatch', 'length', 'max'=>15),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, invoice, counter, credit, employeeno, regularhours, overtimehours, laboramount, overhead, dispatch', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    'employee'=>array(self::BELONGS_TO,'Employees','employeeno'),
                    'invoiceModel'=>array(self::BELONGS_TO,'Invoice','invoice'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'invoice' => 'Invoice',
			'counter' => 'Counter',
			'credit' => 'Credit',
			'employeeno' => 'Technician',
			'regularhours' => 'Regular Hours',
			'overtimehours' => ' Over Time Hours ',
			'laboramount' => 'Labor',
			'overhead' => 'Overhead',
			'dispatch' => 'Dispatch',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
    public function getEmpList()
    {
        $employers = Employees::model()->findAll();
        $employersList = array();
        foreach($employers as $employer){
            $employersList[$employer->empno] = $employer->firstname.
                                           ' '.$employer->middlename.
                                           ' '.$employer->lastname;
        }
       return $employersList;
    }
    
    public function getTotalRegularHours($invoice){
        $sum=Yii::app()->db->createCommand('select sum(regularhours) from tbl_invoice_emp where invoice=:invoice')->bindValue('invoice',$invoice)->queryScalar();
        if($sum!==false){
            return $sum;
        }
    }
    public function getTotalOverhead($invoice){
        $sum=Yii::app()->db->createCommand('select sum(overhead) from tbl_invoice_emp where invoice=:invoice')->bindValue('invoice',$invoice)->queryScalar();
        if($sum!==false){
            return $sum;
        }
    }
    
}